<?php

namespace app\admin\controller\dashboards;

use app\admin\model\exchange\Order;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\StoreConfig;
use think\Session;

/**
 * 统计图表示例
 *
 * @icon   fa fa-charts
 * @remark 展示在FastAdmin中使用Echarts展示丰富多彩的统计图表
 */
class Store extends Backend
{
    protected $model = null;
    protected $agent_id = -1;
    protected $store_no = -1;
    protected $admin_id = -1;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AdminLog');
        $user_info = Session::get('admin');//获取当前登录用户信息
        $this->agent_id = $user_info['agent_id'];
        $this->store_no = $user_info['store_no'];
        $this->admin_id = $user_info['id'];
    }

    /**
     * 查看
     */
    public function index()
    {
        $lingdian = strtotime(date('Y-m-d 00:00:00'));//后面要改成根据配置来获取时间
        $llimitorder = StoreConfig::where('name','order_limit')->where('store_no',$this->store_no)->find();
        if($llimitorder['value'])
        {
            $lingdian =  strtotime('-'.$llimitorder['value'].' hours');
        }
        //获取当前订单情况
        $totle_pay_arr = [];
        $totle_num_arr = [];
        $totle_pay_wx_arr = [];
        $totle_pay_zfb_arr = [];
        $all_pay_order_tmp = Order::where('store_no',$this->store_no)
            ->where('is_pay',2)
            ->where('createtime','>',$lingdian)
            ->where('is_delete',0)//is_delete只在店铺中生效
            ->select();
        if($all_pay_order_tmp){
            foreach ($all_pay_order_tmp as $item){
                $totle_pay_arr[] = $item->total_pay_price;
                $totle_num_arr[] = 1;
                if($item->pay_type == 1){
                    $totle_pay_wx_arr[] = $item->total_pay_price;
                }elseif ($item->pay_type == 2){
                    $totle_pay_zfb_arr[] = $item->total_pay_price;
                }
            }
        }
        $totle_pay = array_sum($totle_pay_arr);
        $totle_pay_wx = array_sum($totle_pay_wx_arr);
        $totle_pay_zfb = array_sum($totle_pay_zfb_arr);
        $totle_num = count($totle_num_arr);

        //当前用户信息
        $today_user = 0;
        $today_user_tmp = User::where('store_no',$this->store_no)->where('createtime','>',$lingdian)->select();//今天注册数
        if($today_user_tmp){
            $today_user = count($today_user_tmp);
        }
        $all_user = 0;
        $all_user_tmp = User::where('store_no',$this->store_no)->select();//总注册数
        if($all_user_tmp){
            $all_user = count($all_user_tmp);
        }

        //获取前7天订单汇总报告
        $sen_date = date('Y-m-d',strtotime('-7 day'));
        $totle_report_pay = [];
        $totle_report_num = [];
        $all_report_order_tmp = \app\admin\model\report\Order::where('store_no',$this->store_no)->where('date','>',$sen_date)->select();
        if($all_report_order_tmp){
            foreach ($all_report_order_tmp as $value){
                $totle_report_pay_arr[$value->date][] = $value->pay_price;
                $totle_report_num_arr[$value->date][] = $value->pay_num;

                $totle_report_pay[$value->date] = array_sum($totle_report_pay_arr[$value->date]);
                $totle_report_num[$value->date] = array_sum($totle_report_num_arr[$value->date]);
            }
        }
        $totle_report_pay_key = '';
        $totle_report_pay_val = '';
        if($totle_report_pay){
            foreach ($totle_report_pay as $kk=>$vv){
                $totle_report_pay_key_arr[] = $kk;
                $totle_report_pay_val_arr[] = $vv;
            }
            $totle_report_pay_key = json_encode($totle_report_pay_key_arr);
            $totle_report_pay_val = json_encode($totle_report_pay_val_arr);
        }
        $totle_report_num_key = '';
        $totle_report_num_val = '';
        if($totle_report_num){
            foreach ($totle_report_num as $kk=>$vv){
                $totle_report_num_key_arr[] = $kk;
                $totle_report_num_val_arr[] = $vv;
            }
            $totle_report_num_key = json_encode($totle_report_num_key_arr);
            $totle_report_num_val = json_encode($totle_report_num_val_arr);
        }

//        var_dump($totle_report_pay);
//        var_dump($totle_report_num);die;

        $this->view->assign("total_pay", $totle_pay);
        $this->view->assign("total_num", $totle_num);
        $this->view->assign("total_pay_wx", $totle_pay_wx);
        $this->view->assign("total_pay_zfb", $totle_pay_zfb);
        $this->view->assign("today_user", $today_user);
        $this->view->assign("all_user", $all_user);
        $this->view->assign("totle_report_pay_key", $totle_report_pay_key);
        $this->view->assign("totle_report_pay_val", $totle_report_pay_val);
        $this->view->assign("totle_report_num_key", $totle_report_num_key);
        $this->view->assign("totle_report_num_val", $totle_report_num_val);

        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }

    //admin/command/Storeorderreport
    public function exs()
    {
        //根据店铺获取前一天订单数据
        $yestodaytime_tmp = strtotime('-1 day');
        $yestodaydate = date('Y-m-d',$yestodaytime_tmp);
        $yestodaytime = strtotime(date('Y-m-d 00:00:00',$yestodaytime_tmp));
        $now_date  = date('Y-m-d 00:00:00');
        $now_date_time = strtotime($now_date);

        $all_order_tmp = Order::where('createtime','>=',$yestodaytime)->where('createtime','<',$now_date_time)->where('is_pay',2)->select();
//        var_dump($all_order_tmp);die;
        if($all_order_tmp){
            $all_order = [];
            $all_save_info = [];
            foreach ($all_order_tmp as  $item){
                if(isset($item->store_no) && $item->store_no){
                    $tmp_store_no = $item->store_no.'##@##'.$item->agent_id;
                    $all_order[$tmp_store_no]['pay_price'][] = $item->total_pay_price;
                    $all_order[$tmp_store_no]['pay_num'][] = 1;
                    if($item->is_pay == 1){
                        //退款
                        $all_order[$tmp_store_no]['refund_price'][] = $item->total_pay_price;
                        $all_order[$tmp_store_no]['refund_num'][] = 1;
                    }

                    if($item->is_pay == 2){
                        //已付款
                        $all_order[$tmp_store_no]['real_price'][] = $item->total_pay_price;
                        $all_order[$tmp_store_no]['real_num'][] = 1;
                    }

                    unset($tmp_store_no);
                }
            }
//            var_dump($all_order);die;
            if ($all_order){
                foreach ($all_order as $key=>$val){
                    $tmp['store_no'] = 0;
                    $tmp['agent_id'] = 0;
                    $store_no_agent_id_arr = explode('##@##',$key);
                    if(isset($store_no_agent_id_arr[0]) && $store_no_agent_id_arr[0]){
                        $tmp['store_no'] = $store_no_agent_id_arr[0];
                    }
                    if(isset($store_no_agent_id_arr[1]) && $store_no_agent_id_arr[1]){
                        $tmp['agent_id'] = $store_no_agent_id_arr[1];
                    }
                    if(!$tmp['store_no'] || !$tmp['agent_id']){
                        continue;
                    }
                    $tmp['pay_price'] = isset($val['pay_price']) && $val['pay_price'] ? array_sum($val['pay_price']):0;
                    $tmp['pay_num'] = isset($val['pay_num']) && $val['pay_num'] ? count($val['pay_num']):0;
                    $tmp['refund_price'] = isset($val['refund_price']) && $val['refund_price'] ? array_sum($val['refund_price']):0;
                    $tmp['refund_num'] = isset($val['refund_num']) && $val['refund_num'] ? count($val['refund_num']):0;
                    $tmp['real_price'] = isset($val['real_price']) && $val['real_price'] ? array_sum($val['real_price']):0;
                    $tmp['real_num'] = isset($val['real_num']) && $val['real_num'] ? count($val['real_num']):0;
                    $tmp['date'] = $yestodaydate;

                    $all_save_info[] = $tmp;

                    unset($tmp);
                }
            }

            $order_report_model = new \app\admin\model\report\Order();
            //删除原来数据
            $order_report_model->where('date',$yestodaydate)->delete();

            //新增数据
            if($all_save_info){
                $order_report_model->saveAll($all_save_info);
            }

        }

    }
}
